<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 4.3.2 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008 - 2010, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * CodeIgniter URL Helpers
 *
 * @package		CodeIgniter
 * @subpackage	Helpers
 * @category	Helpers
 * @author		ExpressionEngine Dev Team
 * @link		http://codeigniter.com/user_guide/helpers/url_helper.html
 */

// ------------------------------------------------------------------------

/**
 * Site URL
 *
 * Create a local URL based on your basepath. Segments can be passed via the
 * first parameter either as a string or an array.
 *
 * @access	public
 * @param	string
 * @return	string
 */
if ( ! function_exists('convert_2_utf8'))
{
	function convert_2_utf8($buffer)
	{
		if (is_utf8($buffer)) {
			return stripcslashes($buffer);
		}
		else {
			return utf8_encode(stripcslashes($buffer));
		}
	}
}

/**
 * Site URL
 *
 * Create a local URL based on your basepath. Segments can be passed via the
 * first parameter either as a string or an array.
 *
 * @access	public
 * @param	string
 * @return	string
 */
if ( ! function_exists('is_utf8'))
{
	function is_utf8($string) {
        return preg_match('%^(?:
              [\x09\x0A\x0D\x20-\x7E]            # ASCII
            | [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
            |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
            | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
            |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
            |  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
            | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
            |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
        )*$%xs', $string);
	}	
}
if ( ! function_exists('getUser'))
{
	function getUser($redirect = false) {

		$CI = & get_instance();
		$user = $CI->session->userdata('mylogin');
        if($redirect && empty($user)) {
        	$CI->load->helper('url');
        	if($CI->config->item('uri_protocol') == 'ORIG_PATH_INFO')
        		redirect($CI->config->item('admin_base').'auth/?surl='.$_SERVER['ORIG_PATH_INFO'],'location');
        	else
        		redirect($CI->config->item('admin_base').'auth/?surl='.$_SERVER['PATH_INFO'],'location');
			exit;
        } 
	}	
}

if ( ! function_exists('p'))
{
	function p($arr = false, $title = '') {
		echo "<pre>";
		echo '<h4>',$title,'</h4>';  
		print_r($arr);
		echo "</pre>";
	}	
}

if ( ! function_exists('TrimIt'))
{
	function TrimIt($text = '', $length = 20) {
		if(strlen($text) > $length) {
			$substr = substr($text,0,($length - 3));
			return $substr.'...';
		}
		return $text;
	}	
}


if ( ! function_exists('FormSelectList'))
{
	function FormSelectList($options= '', $selected='',$name='' ,$style= '',$use_value_as_key = false ) {
		if(!empty($options)) {
			$optionString ='';
	//		print "<br>select ed".$selected.'--'.$use_value_as_key;
			foreach($options as $key => $option) {
				if($use_value_as_key) {
					$key = $option;
				}
				
				if($key == $selected && !empty($selected)) {
					$optionString.= '<option value="'.$key.'" selected="selected">'.$option.'</option>'; 
				} else {
					$optionString.= '<option value="'.$key.'">'.$option.'</option>';
				}	
			}
			$return = '<select id="'.$name.'" name="'.$name.'"'.$style.'>'.$optionString.'</select>';
			return $return;
		}
	}	
}

/**
*
* Created in July 07, 2009, by Vicente Russo Neto <vicente@vrusso.com.br>
*
* @param    integer $date - the date to be processed
* @param    string  $what - what piece of date to process, day, month or year
* @param    integer $value - how much will be increased or decreased
* @param    string  $return_format - 'mysql' format or 'timestamp' format
* @author   Vicente Russo Neto <vicente@vrusso.com.br>
* @return   string|boolean
* @version  0.1
*
* Description: This class can add or subtract days, months or years and return the result. Created for
* PHP Framework CodeIgniter (www.codeigniter.com). Tested on 1.7.1.
*
* Usage:
* $this->load->library('dateoperations');
* echo date_subtract('2009-07-01','day',1); // Prints: 2009-06-30
* echo date_sum('2009-07-01','year',1); // Prints: 2010-07-01
* echo date_subtract('2009-07-01','day',15); // Prints: 2009-06-16
*/
   
    function date_sum($date,$what=FALSE,$value,$return_format='mysql') {
        list($year, $month, $day) = split("-", $date);
        if ($what!='day' && $what!='month' && $what!='year') return false;
       
        if ($what=='day')   $day    = $day + intval($value);
        if ($what=='month')     $month  = $month + intval($value);
        if ($what=='year')  $year   = $year + intval($value);
        $date_tmp = mktime(0, 0, 0, $month, $day, $year);    
           
        if ($return_format=='mysql') {
            $date_tmp = date('Y-m-d', $date_tmp);
        } elseif (!$return_format=='timestamp') {
            return false;  
        }
                       
        return $date_tmp;
       
    }
   
   
    function date_subtract($date,$what=FALSE,$value,$return_format='mysql') {
        list($year, $month, $day) = split("-", $date);
        if ($what!='day' && $what!='month' && $what!='year') return false;    
       
        if ($what=='day')   $day    = $day - intval($value);
        if ($what=='month')     $month  = $month - intval($value);
        if ($what=='year')  $year   = $year - intval($value);
       
        $date_tmp = mktime(0, 0, 0, $month, $day, $year);    
       
        if ($return_format=='mysql') {
            $date_tmp = date('Y-m-d', $date_tmp);
        } elseif (!$return_format=='timestamp') {
            return false;  
        }
        return $date_tmp;
    }
    
    function datediff($high,$low,$period='day')
	{
	   switch($period)
	   {
	      case 'day': $period = 86400; break;
	      case 'month': $period = 2629743.83; break;
	      case 'year': $period = 31556926; break;
	   }
	   return (int)((strtotime($high)-strtotime($low))/$period+1);
	}
	
	function getTransmission() {
		$transmissions = array("Select Transmission","M" => "Manual","A" =>  "Automatic", "SA" => "Semi Automatic");
		return $transmissions;	
	}
	
	function getMonths(){
		$months = array("MONTH","Jan","Feb", "Mar","Apr", "May", "Jun", "Jul", "Aug","Sep", "Oct", "Nov", "Dec");
		return $months;
	}
	
	function getFuel(){
		$fuel = array("Select Fuel Type","P" => "Petrol","D" => "Diesel","E" => "Electric", "CNG" => "CNG","LPG" => "LPG");
		return $fuel;
		
	}
	
	function getUserType(){
		$type = array("Select User Type","I" => "Individual", "D" => "Dealer" );
		return $type;
	}
	
	function getOwner() {
		$arr = array("Select Owners","1","2", "3","4", "5 or more");
		return $arr;
	}
	
	function formatNumber($price ='',$withdecimal=false) {
		if($price ==0) return false;
		if($withdecimal) {
			$formatted = sprintf("%d.00",$price);
		} else {
			$formatted = sprintf("%d",$price);
		}
		return $formatted;
	}
	
	function formatPrice($price ='',$withdecimal=false) {
		if($price ==0) return false;
		if($withdecimal) {
			$formatted = sprintf("%d.00",$price);
		} else {
			$formatted = sprintf("%d",$price);
		}
		return '<del>&#2352;</del>&nbsp;'.$formatted;
	}
	
	function formatText($text = '', $nohtml = false, $length=150,$extender='&nbsp;...') {
		if(empty($text)) return false;
		if($nohtml) {
			$text = strip_tags($text);
		}
		if(strlen($text) > 150) {
			$str = substr($text,0,$length).$extender;		
		} else {
			$str = $text;
		}
		return $str;
	}
	
	function getCarBudget() {
		$arr= array("Select Budget",'Upto 1 Lakh', '1 - 2 Lakhs', '2 - 3 Lakhs', '3 - 4 Lakhs', '4 - 5 Lakhs',  '5 - 8 Lakhs',  '8 - 10 Lakhs', '10 - 12 Lakhs', '12 - 15 Lakh', '15 Lakhs or more');
		return $arr;
	}
	
	function getCarKilometers(){
		$arr = array("Select Kms", "Up to 5000 kms","5000-15000 kms", "15000-30000 kms", "30000-50000 kms", "50000-80000 kms", "80000 kms or above");
		return $arr;
	}
	function getPageList(){
		$arr = array("10" => 10, "20" =>20, "50" =>50);
		//$arr = array(0,1,2,3);
		return $arr;
	}
	
	function Encode($arr = array()) {
		if(empty($arr)) return false;
		$str = serialize($arr);
		return base64_encode($str);	
	}
	
	function Decode($str = '') {
		if(empty($str)) return false;
		$decoded = base64_decode($str);
		return unserialize($decoded);
	}
	
	function isAjax(){
		return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
	}
	
	function formatDate($date = '') {
		if(empty($date)) return '';
		return date('M d, Y',strtotime($date));
	}
	
	function getIPAddress(){
		return $_SERVER['REMOTE_ADDR']; 
	}
	
	function getLangText($lang_code ='', $selfobject = '') {
		if(empty($lang_code) || empty($selfobject)) return ;
		return $selfobject->lang->line($lang_code,$selfobject->config->item('language'));
	}

/* End of file url_helper.php */
/* Location: ./system/helpers/url_helper.php */